package com.spark.incoicings.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.spark.incoicings.service.DictDataService;
import com.spark.incoicings.service.InputInvoiceService;
import com.spark.incoicings.service.OrderService;
import com.spark.incoicings.service.ProcuredSupplierService;
import com.spark.invoicings.entity.*;
import com.spark.invoicings.utils.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 *
 * 进项发票
 *
 */
@RestController
@RequestMapping("/SalesReceipt")
public class InputInvoiceController {
    @Resource
    private ProcuredSupplierService pss;
    @Resource
    private OrderService os;
    @Resource
    private InputInvoiceService iis;
    @Resource
    private DictDataService ds;

    /**
     * 进项发票条件分页查询
     * */
    @GetMapping("/selectInputInvoice")
    public Result selectInputInvoice(
            @RequestParam(defaultValue = "1")Integer pageNum,
            @RequestParam(defaultValue = "6")Integer pageSize,
            @RequestParam(value = "supplierId",required = false) Integer supplierId,
            @RequestParam(value = "invoiceStatusId",required = false) Integer invoiceStatusId){
        PageHelper.startPage(pageNum,pageSize);
        Map<String,Object> map=new HashMap<>();
        map.put("supplierId",supplierId);
        map.put("invoiceStatusId",invoiceStatusId);
        PageHelper.orderBy("invoice_id desc");
        List<InputInvoice> inputInvoices = iis.selectInputInvoice(map);
        for (InputInvoice i:inputInvoices){
            i.setDictData(Arrays.asList(
                    ds.getById(i.getInvoiceStatusId()),
                    ds.getById(i.getInvoiceTypeId())
            ));
            i.setProcuredSupplier(pss.getById(i.getSupplierId()));
            i.setOrder(os.getById(i.getPurchaseOrderId()));
        }
        PageInfo<InputInvoice> page=new PageInfo<>(inputInvoices);
        return Result.success(200,"success",page);
    }

    @GetMapping("/InputInvoiceById")
    public Result InputInvoiceById(Integer id){
        InputInvoice i = iis.getById(id);i.setDictData(Arrays.asList(
                ds.getById(i.getInvoiceStatusId()),
                ds.getById(i.getInvoiceTypeId())
        ));
        i.setProcuredSupplier(pss.getById(i.getSupplierId()));
        i.setOrder(os.getById(i.getPurchaseOrderId()));
        return Result.success(200,"success",i);
    }

    /**
     * 前端需要的数据发票状态、供应商、发票类型
     * */
    @GetMapping("/selectFData")
    public Result selectFData(){
        Map<String, Object> result = new HashMap<>();
        result.put("invoice_status",ds. selectDictType("invoice_status"));
        result.put("list",os.list());
        result.put("invoice_type ",ds.selectDictType("invoice_type") );
        return Result.success(200, "success", result);
    }

    /**
     * 根据供应商id查询采购订单
     * */
    @GetMapping("/selectGOrder")
    public Result selectGOrder(Integer supplierId){
        List<Order> orders = os.selectOrderId(supplierId);
        return Result.success(200, "success", orders);
    }

    /**
     * 添加进项发票
     * */
    @PostMapping("/saveInputInvoice")
    public Result saveInputInvoice(@RequestBody InputInvoice inputInvoice){
        Order o = os.getById(inputInvoice.getPurchaseOrderId());
        double yhp=inputInvoice.getInvoiceAmount()+o.getReceivedAmount();//已回票金额
        double dhp=o.getTotalAmount()-yhp;//待回票
        o.setReceivedAmount(yhp);
        o.setPendingReceiptAmount(dhp);
        os.updateById(o);
        iis.save(inputInvoice);
        return Result.success();
    }

    /**
     * 修改进项发票
     * */
    @PostMapping("/updateInputInvoice")
    public Result updateInputInvoice(@RequestBody InputInvoice inputInvoice){
        iis.updateById(inputInvoice);
        return Result.success();
    }
}
